layui.use(['table', 'form', 'layer', 'laydate', 'laytpl', 'element'], function() {
	var table = layui.table;
	form = layui.form,
	$ = layui.jquery,
	laydate = layui.laydate,
	laytpl = layui.laytpl,
	element = layui.element;
	
	table.render({
		elem: '#editcla',
		id:'classid',
		url : '../sysclassmanagement/getclassbystrwhere',
		title: '查询教师表',
		skin: 'line',
		height: 'full-130',
		even: true,
		cols: [
			[{
				field: 'classNumber',
				title: '班级编号',
				align: "center"
			}, {
				field: 'className',
				title: '班级名称',
				align: "center"
			},{
				field: 'gradename',
				title: '年级',
				align: "center"
			}, {
				field: 'gradeYear',
				title: '入学年份',
				align: "center"
			}, {
				field: 'schoolName',
				title: '学校',
				align: "center"
			}, {
				field: 'period',
				title: '阶段',
				align: "center"
			}, {
				title: '操作',
				toolbar: '#barDemo',
				align: "center"
			}]
		],
		page: {
			layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
			groups: 5,
			limit: 10,
			limits: [10, 20, 30, 40, 50],
			theme: '#1E9FFF',						
		}
	});
	
	$("#selclass").click(function(){
		var grade=$("#selgradename").val();
		var selschoolid = $("#selschool").val();
		if(selschoolid == 00 || selschoolid == null){
			selschoolid = "";
		}
		if(grade==00 || grade == null){
			grade="";
		}
		var idorname = $.trim($("#selclaidorname").val());
		$("#editFrom").hide();
		table.render({
			elem: '#editcla',
			id:'classid',
			url : '../sysclassmanagement/getclassbystrwhere?classid='+idorname+"&grade="+grade + "&schoolid="+selschoolid,
			title: '查询教师表',
			skin: 'line',
			height: 'full-130',
			even: true,
			cols: [
				[{
					field: 'classNumber',
					title: '班级编号',
					align: "center"
				}, {
					field: 'className',
					title: '班级名称',
					align: "center"
				},{
					field: 'gradename',
					title: '年级',
					align: "center"
				}, {
					field: 'gradeYear',
					title: '入学年份',
					align: "center"
				}, {
					field: 'schoolName',
					title: '学校',
					align: "center"
				}, {
					field: 'period',
					title: '阶段',
					align: "center"
				}, {
					title: '操作',
					toolbar: '#barDemo',
					align: "center"
				}]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
			}
		});
	});
	
	//监听行工具事件
	table.on('tool(editcla)', function(obj) {
		var data = obj.data;
		/*查看班级信息详情*/
		if(obj.event === 'btnselclass') {
			$("#txtclaid").text(data.classid);
			$("#txtclaname").text(data.className);
			$("#txtclagrade").text(data.gradename);
			$("#txtclaper").text(data.period);
			$("#txtclassnumber").text(data.classNumber);
			$("#txtschool").text(data.schoolName);
			
			layer.open({
		        type: 1, 
		        title: '班级信息详情',
		        area: ['640px', '480px'],
		        shade: 0.8,
		        content: $('#classdea'),
		        btn: ['返回'], 
		        yes: function(){
		          layer.closeAll();
		          $(".classdea-con").css("display","none");
		        },
		        cancel: function(){ 
				   $(".classdea-con").css("display","none");
				}
		    });
		}
		/*修改班级信息*/
		else if(obj.event === 'edit') {
			$("#inpclassid").val(data.classid);
			$("#inpclaname").val(data.className);
			$("#inpclayear").val(data.gradeYear);
			$("#inpclassnumber").val(data.classNumber);
			$("#inpclaper").val(data.period);
			$("#inpschool").val(data.schoolName);
            var index=layer.open({
				type: 1, 
			    title: '修改班级信息',
		        area: ['830px', '95%'],
		        shade: 0.8,
		        maxmin: true,
		        content: $('#editclaFrom'),
		        cancel: function(){ 
				   $(".editclalayer-con").css("display","none");
				}
		    });
	    	layer.full(index);

	    	/*班级信息修改js*/
	    	$("#btneditcla").click(function(){
	    		var classid=$("#inpclassid").val();
	    		var className=$("#inpclaname").val();
	    		var gradeYear=$("#inpclayear").val();
	    		$.ajax({
	    			url: '../sysclassmanagement/updateclass',  
	    	        type: 'get', 
	    	        dataType: 'json',  
	    	        data:{classid:classid,className:className,gradeYear:gradeYear},
	    	        success: function (data) {
	    	          if (data.code == 10001) {
		    	        	 layer.alert('班级信息修改成功！', {icon: 6});
	    	        	 /*关闭弹窗*/
	    	        	  setTimeout(function(){
	    	        		  layer.close(index);
	    	        		  $(".editclalayer-con").css("display","none");
	    	        		  }, 3000);
	    	        	  table.reload("classid", { //此处是上文提到的 初始化标识id
	    		                where: {
	    		                	keyword:data.code=='10001'
	    		                }
	    		          });	
	    	          } else {  
		    	        	 layer.alert('班级信息修改失败！', {icon: 2});
	    	        	  $("#editFrom").hide();
	    	          }  
	    	        }
	    		});  
	    	});	    	
		}
		if(obj.event === 'delete') {
			layer.confirm('你确定要删除吗？', {
	    		  btn: ['确定','取消'] 
	    		}, function(){
	    	    	$.ajax({
	        			url: '../sysclassmanagement/deleteclass',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{classid: data.classid},
	        	        success: function (getdata) {
	        	          if (getdata.code == 10001) { 
	        	        	  layer.alert(getdata.msg, {icon: 1});
	        	        	  table.reload("classid", { //此处是上文提到的 初始化标识id
					              where: {
					            	  keyword:getdata.code == '10001'
					              }
				            });
	        	          }else { 
	        	        	  layer.alert(getdata.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){
	        	        	layer.alert(getdata.msg, {icon: 2}); 
	        	        }
	        		});
	    		}, function(){ }
	    	);
		}
	});
	
});